package com.pet.petadoption.adoption.controller;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.pet.petadoption.adoption.biz.impl.AdoptionBizImpl;
import com.pet.petadoption.adoption.entity.Adoption;
import com.pet.petadoption.pet.entity.Pet;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.rmi.runtime.Log;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("adoption")
@Log4j2
@CrossOrigin(origins = "http://localhost:8080",maxAge = 36000)
public class AdoptionController {
    @Autowired
    AdoptionBizImpl adoptionBiz;

    @RequestMapping(value = "/requestAd")
    @ResponseBody
    public Boolean requestAd(String pet_id,String adopter_id){
        return adoptionBiz.request(Integer.valueOf(pet_id),Integer.valueOf(adopter_id));
          }


    @RequestMapping(value = "/getAllAdoption")
    @ResponseBody
    public Map<String,Object> getAllAdoption(Integer index,Boolean isAdopted) {
//        log.warn(index);
        Integer newIndex = index;
        if (newIndex == null) {
            newIndex = 1;
        }
        int size = 5;
//        log.warn(newIndex);
        List<Adoption> list = adoptionBiz.findAllAdoption(newIndex,size,isAdopted);
        System.out.println(list.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("list", list);
        return map;
    }



    @RequestMapping(value = "/delAdoptionById")
    @ResponseBody
    public Boolean delAdoptionById(Integer id){
        return adoptionBiz.delAdoptionById(id);
    }


    @RequestMapping(value = "/delNoAdoptionById")
    @ResponseBody
    public Boolean delNoAdoptionById(String  id){
        return adoptionBiz.delNoAdoptionById(Integer.valueOf(id));
    }


    @RequestMapping(value = "/findById")
    @ResponseBody
    public List<Adoption> findById(Integer id,Boolean isAdopted){

       Adoption adoption =adoptionBiz.findById(id,isAdopted);
       List<Adoption> list = new ArrayList<>();
       if(adoption==null){
           return list;
       }else{
           list.add(adoption);
           return list;
       }

    }

    @RequestMapping(value = "/findByUname")
    @ResponseBody
    public List<Adoption> findByUname (String uname , Boolean isAdopted){
        return adoptionBiz.findByUname(uname,isAdopted);
    }

    @RequestMapping(value = "/accessAdoption")
    @ResponseBody
    public boolean accessAdoption(String id){
        Integer row = adoptionBiz.accessAdoption(Integer.valueOf(id));
        return row ==1;
    }

}
